#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>

struct MyStruct {
    int a;
    char b;
    float c;
};

int main() {
    struct MyStruct *ptr = malloc(sizeof(struct MyStruct));
    if (ptr == NULL) {
        perror("malloc");
        return 1;
    }

    ptr->a = 10;
    ptr->b = 'X';
    ptr->c = 3.14;
    
    printf("struct size: %ld\n", sizeof(struct MyStruct));
    printf("a offset: %ld, b offset: %ld, c offset: %ld\n", offsetof(struct MyStruct, a), offsetof(struct MyStruct, b), offsetof(struct MyStruct, c));

    // 通过指针偏移访问结构体成员
    float *c_ptr = (float *)((char *)ptr + offsetof(struct MyStruct, c));
    printf("c = %.2f\n", *c_ptr);

    free(ptr);

    return 0;
}